//
//  WBWelcomeView.swift
//  Weibo
//
//  Created by kobe on 2016/12/25.
//  Copyright © 2016年 kobe. All rights reserved.
//

import UIKit
import SDWebImage

class WBWelcomeView: UIView {
    
    @IBOutlet weak var iconView: UIImageView!
    
    @IBOutlet weak var tipLabel: UILabel!

    //底部约束
    @IBOutlet weak var bottomCons: NSLayoutConstraint!
    
    @IBOutlet weak var iconWidth: NSLayoutConstraint!
    
    class func welcomeView() -> WBWelcomeView {
        let nib = UINib(nibName: "WBWelcomeView", bundle: nil)
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! WBWelcomeView
        v.frame = UIScreen.main.bounds
        
        return v
        
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        //只是刚刚从 xib 的二进制文件将视图数据加载完成 还没有和代码练习建立起关系，所以不要开发时不要在这个方法中处理UI
    }
    
    override func awakeFromNib() {

        guard let urlStr = WBNetworkManager.shared.userAccount.avatar_large else {
            return
        }
        let url = URL(string: urlStr)
        iconView.sd_setImage(with: url,placeholderImage:UIImage(named:"avatar_default_big"))
        
        iconView.layer.masksToBounds = true
        iconView.layer.cornerRadius = iconWidth.constant/2
    }
    
    
    //视图被添加到window上 表示视图已经显示
    override func didMoveToWindow() {
        
        //视图是使用自动布局到window上 表示视图已经显示
        //当视图被添加到窗口上时 根据父视图的大小 计算的束值 更新控件位置
        self.layoutIfNeeded()
        bottomCons.constant = bounds.size.height*2/3
        
        //如果控件们的frame还没计算好  所有的约束会一起动画
        UIView.animate(withDuration: 2.0,
                       delay: 0,
                       usingSpringWithDamping: 0.7,
                       initialSpringVelocity: 0,
                       options: [],
                       animations: {
            
            //更新约束
            self.layoutIfNeeded()
            
            }) { (_) in
                UIView.animate(withDuration: 1.0, animations: { 
                    self.tipLabel.alpha = 1
                    }, completion: { (_) in
                        self.removeFromSuperview()
                })
        }
    }

}
